<?php defined('SYSPATH') or die();?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>思途CMS{$coreVersion}</title>
    {template 'stourtravel/public/public_min_js'}
    {php echo Common::getCss('style.css,base.css,base_new.css,../js/jqueryui/jquery-ui.min.css'); }
    {php echo Common::getScript("uploadify/jquery.uploadify.min.js,choose.js,product_add.js,laydate/laydate.js,imageup.js,jquery.upload.js"); }
    {php echo Common::getCss('uploadify.css','js/uploadify/'); }
    {Common::css_plugin('promo.css','promo')}
</head>

<body>

<!--顶部-->
{php Common::getEditor('jseditor','',$sysconfig['cfg_admin_htmleditor_width'],300,'Sline','','print',true);}
<table class="content-tab">
    <tr>
        <td width="119px" class="content-lt-td" valign="top">
            {template 'stourtravel/public/leftnav'}
            <!--右侧内容区-->
        </td>
        <td valign="top" class="content-rt-td" style="overflow:auto;">
            <form id="product_fm">
                <div class="cfg-header-bar">
                    <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="window.location.reload()">刷新</a>
                </div>
                <div class="product-add-div" id="content_basic">
                    <ul class="info-item-block">
                        <li>
                            <span class="item-hd">活动名称：</span>
                            <div class="item-bd">
                                <input type="text" name="title" data-required="true" value="{$info['title']}" class="input-text w300">
                                <input type="hidden" id="promo_id" name="promo_id" value="{$info['id']}">
                            </div>
                        </li>
                        <li>
                            <span class="item-hd">活动时间：</span>
                            <div class="item-bd">
                                <div class="choose-start-date w100">
                                    <input type="text" name="start_date" autocomplete="off" class="date-text start-date w100" value="{$info['start_date']}">
                                    <i class="date-icon"></i>
                                </div>
                                <span class="item-text">至</span>
                                <div class="choose-start-date w100">
                                    <input type="text" name="end_date" autocomplete="off" class="date-text end-date w100" value="{$info['end_date']}">
                                    <i class="date-icon"></i>
                                </div>
                            </div>
                        </li>
                        <li>
                            <span class="item-hd">弹窗关闭时间：</span>
                            <div class="item-bd">
                                <input type="text" name="auto_time" class="input-text w50" value="{$info['auto_time']}">
                                <span class="item-text">秒</span>
                            </div>
                        </li>
                        <li>
                            <span class="item-hd">推送频次：</span>
                            <div class="item-bd">
                                <label class="radio-label mr-10"><input type="radio" name="frequency" value="1" {if $info['frequency']==1}checked{/if}>一直推送</label>
                                <label class="radio-label w100"><input type="radio" name="frequency" value="2" {if $info['frequency']==2}checked{/if}>每天推荐一次</label>
                            </div>
                        </li>
                        <li>
                            <span class="item-hd">推送内容：</span>
                            <div class="item-bd">
                                <div class="type-switch">
                                    <label class="radio-label w100"><input name="type" type="radio" value="1" {if $info['type']==1}checked{/if}>单图广告</label>
                                    <label class="radio-label w100"><input name="type" type="radio" value="2" {if $info['type']==2}checked{/if}>多图广告</label>
                                    <label class="radio-label w100"><input name="type" type="radio" value="3" {if $info['type']==3}checked{/if}>优惠券</label>
                                    <label class="radio-label w100"><input name="type" type="radio" value="4" {if $info['type']==4}checked{/if}>视频广告</label>
                                </div>
                                <table class="table table-border table-bordered table-striped w900 type-table type-list type-12" {if $info['type']!=1 && $info['type']!=2}style="display: none"{/if}>
                                    <thead>
                                        <tr>
                                            <th class="text-c" style="width: 33%;">广告图片</th>
                                            <th class="text-c" style="width: 33%;">跳转地址</th>
                                            <th class="text-c" style="width: 34%;">操作 <a onclick="add_img()" style="color: #0a8ddf">添加</a></th>
                                        </tr>
                                    </thead>
                                    <tbody class="img-list">
                                    {loop $info['img_url'] $k $img}
                                        <tr class="selected-status">
                                            <td class="text-c img-url-container">
                                                <img style="height: 40px" src="{$img}">
                                                <input type="hidden" name="img_url[]" value="{$img}">
                                            </td>
                                            <td class="text-c jump-url-container">
                                                {if $info['jump_url'][$k]['data']['path']}
                                                <span>{$info['jump_url'][$k]['data']['path']}</span>
                                                {else}
                                                <span>{$info['jump_url'][$k]['data']['dis']}</span>
                                                {/if}
                                                <input type="hidden" name="jump_url[]" value="{$info['jump_url'][$k]['url_code']}">
                                            </td>
                                            <td class="text-c">
                                                <a onclick="upload_img($(this),1)" class="mr-5" style="color: #0a8ddf">上传图片</a>
                                                <a onclick="add_link($(this))" class="mr-5" style="color: #0a8ddf">设置地址</a>
                                                <a onclick="$(this).parent().parent().remove()" class="mr-5" style="color: #dd514c">删除</a>
                                            </td>
                                        </tr>
                                    {/loop}
                                    </tbody>
                                </table>
                                <div class="type-3 type-list" {if $info['type']!=3}style="display: none"{/if}>
                                    <table class="table table-border table-bordered table-striped w900 type-table">
                                        <thead>
                                            <tr>
                                                <th class="text-l" style="width: 40%;">名称</th>
                                                <th class="text-c" style="width: 20%;">数量</th>
                                                <th class="text-c" style="width: 20%;">有效期</th>
                                                <th class="text-c" style="width: 20%;">操作 <a onclick="select_coupon()" style="color: #0a8ddf">添加</a></th>
                                            </tr>
                                        </thead>
                                        <tbody class="coupon-list">
                                        {loop $info['coupon_list'] $v}
                                            <tr>
                                                <td>
                                                    {$v['name']}
                                                    <input type="hidden" name="coupon_id[]" value="{$v['id']}">
                                                </td>
                                                <td class="text-c">{$v['totalnumber']}</td>
                                                <td class="text-c">{$v['validity_period']}</td>
                                                <td class="text-c">
                                                    <a onclick="$(this).parent().parent().remove()" class="mr-5" style="color: #dd514c">删除</a>
                                                </td>
                                            </tr>
                                        {/loop}
                                        </tbody>
                                    </table>
                                    <div class="mt-10">
                                        <div class="clearfix">
                                            <span class="fl item-text">内容左对齐：</span>
                                            <label class="fl coupon-switch radio-label"><input type="radio" name="coupon_style" value="0" {if $info['coupon_style']==0}checked{/if}>系统默认</label>
                                            <label class="fl coupon-switch radio-label ml-10"><input type="radio" name="coupon_style" value="1" {if $info['coupon_style']==1}checked{/if}>自定义背景图 建议尺寸600*956px</label>
                                        </div>
                                        <div class="custom-bg-block mt-10 coupon-img" {if $info['coupon_style']!=1}style="display: none"{/if}>
                                            <a href="javascript:;" class="custom-bg-btn" onclick="upload_img($(this),2)">
                                                <div class="add-upload" {if $info['coupon_img']}style="display: none"{/if}>
                                                    <i class="upload-icon"></i>
                                                    <div class="upload-txt">上传背景图</div>
                                                </div>
                                                {if $info['coupon_img']}
                                                <img src="{$info['coupon_img']}" class="upload-img">
                                                {else}
                                                <img src="" class="upload-img" style="display: none">
                                                {/if}
                                                <input type="hidden" name="coupon_img" value="{$info['coupon_img']}">
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                <div class="type-4 type-list" {if $info['type']!=4}style="display: none"{/if}>
                                    <div class="mt-3">
                                        <a href="javascript:;" class="btn btn-primary radius size-S" id="uploadVideo">上传视频</a>
                                        <span class="item-text ml-10 c-999">视频大小不超过2M</span>
                                    </div>
                                    <ul class="up-video-list" id="videoContent">
                                        {if $info['video']}
                                        {php}list(,$videoName)=explode('|',$info['video']){/php}
                                        <li>
                                            <input name="video" type="hidden" value="{$info['video']}">
                                            <span class="v-name">{$videoName}</span>
                                            <a href="javascript:;" class="btn-link ml-20" onclick="videoNode()">删除</a>
                                        </li>
                                        {/if}
                                    </ul>
                                </div>
                            </div>
                        </li>
                        <li>
                            <span class="item-hd">展示页面：</span>
                            <div class="item-bd">
                                <div>
                                    <label class="radio-label w100 page-type"><input type="radio" name="page_type" value="0" {if $info['page_type']!=1}checked{/if}>首页</label>
                                    <label class="radio-label w100 page-type"><input type="radio" name="page_type" value="1" {if $info['page_type']==1}checked{/if}>自定义</label>
                                </div>
                                <table class="table table-border table-bordered table-striped w900 type-table page-list-container" {if $info['page_type']!=1}style="display:none"{/if}>
                                    <thead>
                                        <tr>
                                            <th class="text-c" style="width: 33%;">所属模块</th>
                                            <th class="text-c" style="width: 33%;">所属页面</th>
                                            <th class="text-c" style="width: 34%;">操作 <a onclick="add_page()" style="color: #0a8ddf">添加</a></th>
                                        </tr>
                                    </thead>
                                    <tbody class="page-list">
                                    {loop $info['page_link'] $v}
                                        <tr>
                                            <td class="text-c">{$v['name']}</td>
                                            <td class="text-c">
                                                {$v['title']}：{$v['path']}
                                                <input type="hidden" name="page_link[]" value="{$v['path']}">
                                            </td>
                                            <td class="text-c">
                                                <a onclick="$(this).parent().parent().remove()" class="mr-5" style="color: #dd514c">删除</a>
                                            </td>
                                        </tr>
                                    {/loop}
                                    </tbody>
                                </table>
                            </div>
                        </li>
                    </ul>
                </div>
            </form>
            <div class="clear clearfix pt-20 pb-20">
                <a class="btn btn-primary radius size-L ml-115" id="save_btn" href="javascript:;">保存</a>
            </div>
        </td>
    </tr>
</table>

<script>
    function add_page()
    {
        var url = SITEURL+'promo/admin/promo/dialog_page_list?gift_coupon_id=';
        CHOOSE.setSome("选择页面", {width:900,loadWindow:window,loadCallback:page_show}, url, 1)
    }

    function page_show(res, status)
    {
        var html = '';
        for (var i=0; i<res.name.length; i++) {
            html += '<tr>';
            html += '<td class="text-c">'+res.name[i]+'</td>';
            html += '<td class="text-c">'+res.title[i]+'：'+res.path[i];
            html += '<input type="hidden" name="page_link[]" value="'+res.path[i]+'">';
            html += '</td>';
            html += '<td class="text-c"><a onclick="$(this).parent().parent().remove()" class="mr-5" style="color: #dd514c">删除</a></td>';
            html += '</tr>';
        }
        $('.page-list').append(html);
    }

    <!--优惠券操作事件-->
    function select_coupon()
    {
        var url = SITEURL+'promo/admin/promo/dialog_coupon_list?gift_coupon_id=';
        CHOOSE.setSome("选择优惠券", {width:900,loadWindow:window,loadCallback:coupon_show}, url, 1)
    }

    function coupon_show(res, status)
    {
        var coupon_id = res.id;
        var name = res.name;
        var number = res.total_number;
        var validity_period = res.validity_period;
        var html = '';
        for (var i = 0; i < coupon_id.length; i++) {
            html += '<tr>';
            html += '<td>' + name[i] ;
            html += '<input type="hidden" name="coupon_id[]" value="' + coupon_id[i] + '">';
            html += '</td>';
            html += '<td class="text-c">' + number[i] + '</td>';
            html += '<td class="text-c">' + validity_period[i] + '</td>';
            html += '<td class="text-c">';
            html += '<a onclick="$(this).parent().parent().remove()" class="mr-5" style="color: #dd514c">删除</a>';
            html += '</td>';
        }
        $(".coupon-list").append(html);
    }

    <!--图片操作事件-->
    function add_link(obj)
    {
        $('.img-list tr').removeClass('selected-status');
        obj.parent().parent().addClass('selected-status');
        ST.Util.showBox('编辑uniapp导航链接',SITEURL+'uniapp/dialog_nav_link',900,600,null,null,document,{loadWindow:window,loadCallback: saveLink});
    }

    function upload_img(obj,type)
    {
        $('.img-list tr').removeClass('selected-status');
        obj.parent().parent().addClass('selected-status');
        ST.Util.showBox('上传图片', SITEURL + 'image/insert_view', 0,0, null, null, document, {loadWindow: window, loadCallback: Insert});
        function Insert(result,bool){
            var temp =result.data[0].split('$$');
            if(type===1){
                var img_html = '<img style="height: 40px" src="'+temp[0]+'">';
                img_html += '<input type="hidden" name="img_url[]" value="'+temp[0]+'">';
                $('.selected-status .img-url-container').html(img_html);
            }else{
                $('.add-upload').hide();
                $('.upload-img').attr('src',temp[0]).show();
                $('input[name="coupon_img"]').val(temp[0]);
            }
        }
    }

    function saveLink(res, status)
    {
        var ad_type = res.adType;
        var data = '';
        if(res.data[0]){
            res.data[0]["ad_type"] = ad_type;
            data = res.data[0];
        }else{
            res.data['ad_type'] = ad_type;
            data = res.data;
        }
        var json = encodeURIComponent(JSON.stringify(data));
        var img_html  = '';
        var jump_html = '';

        if(ad_type==='1'){
            jump_html += '<span>'+data["dis"]+'</span>';
            jump_html += '<input type="hidden" name="jump_url[]" value="'+json+'">';
        }else if(ad_type==='2'){
            jump_html += '<span>'+data["path"]+'</span>';
            jump_html += '<input type="hidden" name="jump_url[]" value="'+json+'">';
        }else if(ad_type==='3'){
            jump_html += '<span>'+data["path"]+'</span>';
            jump_html += '<input type="hidden" name="jump_url[]" value="'+json+'">';
        }else{
            jump_html += '<span>'+data["path"]+'</span>';
            jump_html += '<input type="hidden" name="jump_url[]" value="'+json+'">';
        }

        $('.selected-status .jump-url-container').html(jump_html);
        var img = $('.selected-status .img-url-container input').val();
        if(data["litpic"] && !img){
            img_html += '<img style="height: 40px" src="'+data["litpic"]+'">';
            img_html += '<input type="hidden" name="img_url[]" value="'+data["litpic"]+'">';
            $('.selected-status .img-url-container').html(img_html);
        }
    }

    function add_img()
    {
        var length = $('.img-list tr').length;
        var type = $("[name='type']:checked").val();
        if(type==='1'){
            if(length>=1){
                ST.Util.showMsg('单图广告，不可添加多张图片！',5);
                return false;
            }
        }else{
            if(length>=10){
                ST.Util.showMsg('最多添加10个广告图！',5);
                return false;
            }
        }
        var html = '<tr>';
        html += '<td class="text-c img-url-container">';
        html += '</td>';
        html += '<td class="text-c jump-url-container">';
        html += '</td>';
        html += '<td class="text-c">';
        html += '<a onclick="upload_img($(this),1)" class="mr-5" style="color: #0a8ddf">上传图片</a> ';
        html += '<a onclick="add_link($(this))" class="mr-5" style="color: #0a8ddf">设置地址</a> ';
        html += '<a onclick="$(this).parent().parent().remove()" class="mr-5" style="color: #dd514c">删除</a> ';
        html += '</td>';
        html += '</tr>';

        $(".img-list").append(html);
    }
    var min_date = "{$min_date}";
    $(function(){
        laydate.render({
            elem: '.start-date',
            min: min_date
        });
        laydate.render({
            elem: '.end-date',
            min: min_date
        });

        $('#uploadVideo').click(function(){
            // 上传方法
            $.upload({
                // 上传地址
                url: SITEURL+'uploader/video',
                // 文件域名字
                fileName: 'video',
                fileType: 'mp4',
                // 其他表单数据
                params: {uploadcookie:"<?php echo Cookie::get('username')?>"},
                // 上传完成后, 返回json, text
                dataType: 'json',
                // 上传之前回调,return true表示可继续上传
                onSend: function() {
                    return true;
                },
                // 上传之后回调
                onComplate: function(info) {
                    if(info.status){
                        $('#videoContent').html('<li><input name="video" type="hidden" value="'+info.data.path+'|'+info.data.name+'"><span class="v-name">'+info.data.name+'</span><a href="javascript:;" class="btn-link ml-20" onclick="videoNode()">删除</a></li>');
                    }
                }
            });
        });

        $('.page-type').on('click',function(){
            var type = $(this).find('input').val();
            if(type==='1'){
                $('.page-list-container').show();
            }else{
                $('.page-list-container').hide();
            }
        });

        $('.coupon-switch').on('click',function(){
            var type = $(this).find('input').val();
            if(type==='1'){
                $('.coupon-img').show();
            }else{
                $('.coupon-img').hide();
            }
        });

        $('.type-switch label').on('click',function(){
            $('.type-list').hide();
            var type = $(this).find('input').val();
            if(type==='1' || type==='2'){
                if(type==='1'){
                    $('.img-list tr').not(':first-child').hide();
                }else{
                    $('.img-list tr').not(':first-child').show();
                }
                $('.type-12').show();
            }else{
                $('.type-'+type).show();
            }
        });
    })
</script>
<script>
    $(document).ready(function(e) {
        $("#save_btn").click(function(e) {
            var type = $("[name='type']:checked").val();
            if(type==='1'){
                $('.img-list tr').not(':first-child').remove();
            }
            $.ajaxform({
                url   :  SITEURL+"promo/admin/promo/ajax_promo_save",
                method  :  "POST",
                form  : "#product_fm",
                dataType  :  "JSON",
                success  :  function(result)
                {
                    console.log(result);
                    if(!result.status) {
                        ZENG.msgbox._hide();
                        ST.Util.showMsg(result.msg,5);
                    } else {
                        $("#promo_id").val(result.id);
                        ST.Util.showMsg('保存成功',4)
                    }
                }
            });
        });
    });
</script>
</body>
</html>
